package com.example.test622.login

import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.common.mvi.BaseView
import com.example.test622.databinding.ActivityLoginBinding
import com.tencent.mmkv.MMKV
@Route(path = "/splash/login")
class LoginActivity : BaseView<ActivityLoginBinding,LoginViewModel>() {
	val map by lazy {
		mutableMapOf("password" to "123","username" to "chen")
	}
	override fun initView() {
		binding.user = map
		binding.btn.setOnClickListener {
			viewModel.intent.trySend(LoginIntent.Login(map["password"]!!,map["username"]!!))
		}
		binding.btn2.setOnClickListener {
			ARouter.getInstance().build("/login/register").navigation()
		}
	}
	
	override fun initData() {
		viewModel.state.observe(this){
			when(it){
				is LoginState.LoginSuccess->{
					val mmkv = MMKV.defaultMMKV()
					mmkv.encode("token", it.user.data.token)
					mmkv.encode("uid",it.user.data.id)
//					Toast.makeText(this, "${mmkv.decodeString("token")}", Toast.LENGTH_SHORT).show()
					ARouter.getInstance().build("/login/main").navigation()
					
				}
				is LoginState.LoginFailed->{
					Toast.makeText(this, "${it.error}", Toast.LENGTH_SHORT).show()
				}
			}
		}
	}
}